﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class DepCheckIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                txtAirline.Text = Session["AIRLINE"].ToString();
                txtAirport.Text = Session["DCS_AIRPORT"].ToString();
                if (Session["FLT_DATE"] != null)
                {
                    try 
                    { 
                        txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
                    }
                    catch(Exception ex)
                    {
                        ;
                    }
                }
                if (Session["CHKIN_NO"] == null)
                {
                    if (Session["BK_ID"] != null)
                    {
                        txtNumber.Text = Session["BK_ID"].ToString();
                        //GridView1.DataBind();
                        //btnRefreshSeatMap_Click(sender, e);
                    }
                }
                else
                    txtNumber.Text = Session["CHKIN_NO"].ToString();

                //if (Request.QueryString["flt_no"] != null)
                //{
                //    txtFltNo.Text = Request.QueryString["flt_no"].ToString();
                //    txtFltDate.Text = Request.QueryString["flt_date"].ToString();
                //}

            }
            lblError.Text = "";
            lblError2.Text = "";
            //btnGateAct.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            //btnCounterAct.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnCheckIn.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAddBag.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnSetBag.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnCheckOut.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void btnRefreshSeatMap_Click(object sender, EventArgs e)
        {
            int ret_code;

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError2.Text = "Please select a passenger.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');


            ret_code = ctlSelectSeat.RetreiveSeatMap(Convert.ToInt64(strArray[0]), Convert.ToInt16(strArray[1]), Convert.ToInt16(strArray[2]), Session["AIRLINE"].ToString(), Session["OFFICE"].ToString(), false);
            switch (ret_code)
            {
                case 0:
                    //lblError.Text = "OK.";
                    break;
                case 1:
                    lblError2.Text = "Ticket not found, or all tickets in the order are checked-in!";
                    return;
                case 2:
                    lblError2.Text = "Seat map not found!";
                    return;
                case 50:
                    lblError2.Text = "Unauthorized airline!";
                    return;
                default:
                    lblError2.Text = "Error:" + ret_code.ToString();
                    return;
            }
            return;

        }

        protected void lbtnQueryOrder_Click(object sender, EventArgs e)
        {
            long tmp;
            if (long.TryParse(txtNumber.Text, out tmp))
            {
                //数字
            }
            else
            {
                //非数字
                lblError.Text = "Please input order number!";
                return;
            }
            //string pax_sn, bk_id;
            hidAirline.Value = txtAirline.Text;
            hidFltDate.Value = txtFltDate.Text;
            hidAirport.Value = txtAirport.Text;//.Trim().ToUpper();

            //txtTicketNo.Text = txtTicketNo.Text.Trim();
            //if (txtTicketNo.Text.Length != 14)
            //{
            //    lblError.Text = "Invalid ticket number!";
            //    return;
            //}
            //pax_sn = txtTicketNo.Text.Substring(txtTicketNo.Text.Length - 3);
            //bk_id = txtTicketNo.Text.Substring(0, txtTicketNo.Text.Length - 3);

            //hidOrderNo2.Value = txtTicketNo.Text.Substring(0, txtTicketNo.Text.Length - 3);
            //hidOrderNo.Value = hidOrderNo2.Value;
            //hidPaxSn.Value = txtTicketNo.Text.Substring(txtTicketNo.Text.Length - 3, 3);

            hidSearchNo.Value = txtNumber.Text;
            hidSearchNo2.Value = hidSearchNo.Value;
            hidSearchWhat.Value = "O";//Order
            hidSearchWhat2.Value = hidSearchWhat.Value;

            Session["FLT_DATE"] = txtFltDate.Text;
            Session["BK_ID"] = hidSearchNo.Value;
            Session["CHKIN_NO"] = hidSearchNo.Value;
            //Session["TKT_NO"] = txtTicketNo.Text;

            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();
            //btnRefreshSeatMap_Click(sender, e);
        }
        protected void btnCheckIn_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            if (chkBlockChkIn.Checked)
            {
                if (Session["ACL"].ToString().IndexOf("CC+") < 0)
                {
                    lblError.Text = "Unauthorized!";
                    return;
                }
            }
            int ret_code;
            string key, row, col;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            string[] strArray2;

            if (chkBlockChkIn.Checked)
            {
                if (Request.Form["SEAT"] == null)
                {
                    lblError.Text = "Please refresh seat map and select a seat.";
                    return;
                }
                key = Request.Form["SEAT"];
                strArray2 = key.Split('|');
                row = strArray2[0];
                col = strArray2[1];
            }
            else
            {
                if (!chkAutoAssign.Checked && Request.Form["SEAT"] == null)
                {
                    lblError.Text = "Please refresh seat map and select a seat.";
                    return;
                }
                else if (!chkAutoAssign.Checked)
                {
                    key = Request.Form["SEAT"];
                    strArray2 = key.Split('|');
                    row = strArray2[0];
                    col = strArray2[1];
                }
                else//auto assign
                {
                    row = "";
                    col = "";
                }
            }


            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";

            cmd.CommandText = "bll_tkt_check_in";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
            para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
            //para = cmd.Parameters.AddWithValue("@flt_date", strArray[2]);
            //para = cmd.Parameters.AddWithValue("@op_airline", strArray[3]);
            //para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[4]);
            //para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
            para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
            //para = cmd.Parameters.AddWithValue("@flt_date", hidFltDate.Value);
            //para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
            para = cmd.Parameters.AddWithValue("@auto_assign", chkAutoAssign.Checked);
            para = cmd.Parameters.AddWithValue("@row", row);
            para = new SqlParameter("@col", SqlDbType.Char, 1);
            para.Value = col;
            cmd.Parameters.Add(para);
            para = cmd.Parameters.AddWithValue("@bag_count", lstBagCount.SelectedValue);
            para = cmd.Parameters.AddWithValue("@bag_weight", txtWeight.Text);
            para = cmd.Parameters.AddWithValue("@force_chk_in", chkBlockChkIn.Checked);
            para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        if (hidSearchWhat.Value == "O")
                            GridView1.DataSource = SqlDataSource1;
                        else
                            GridView1.DataSource = SqlSearchID;
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Trip not found, or inproper status, or ticket expired!";
                        break;
                    case 3:
                        lblError.Text = "Flight not open for checking-in!";
                        break;
                    case 5:
                        lblError.Text = "Seat not found, or occupied!";
                        break;
                    case 9:
                        lblError.Text = "Check-in failed! Maybe occupied.";
                        break;
                    case 10:
                        lblError.Text = "Check-in failed! Check seating rules.";
                        break;
                    case 11:
                        lblError.Text = "Update tickets failed! Please check pax status.";
                        break;
                    case 12:
                        lblError.Text = "Can not find a available seat.";
                        break;
                    case 13:
                        lblError.Text = "Infant should check-in with his/her parent's seat number.";
                        break;
                    case 14:
                        lblError.Text = "Please select a seat!";
                        break;
                    case 15:
                        lblError.Text = "Release original reserved seat failed! Maybe the seat is not belong to this ticket.";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized!";
                        break;
                    default:
                        lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        //protected void btnCheckOut_Click(object sender, EventArgs e)
        //{
        //    int ret_code;
        //    string key;
        //    string[] strArray;
        //    if (Request.Form["KEY"] == null)
        //    {
        //        lblError.Text = "Please select a record.";
        //        return;
        //    }
        //    key = Request.Form["KEY"];
        //    strArray = key.Split('|');

        //    SqlCommand cmd = new SqlCommand();
        //    lblError.Text = "";

        //    cmd.CommandText = "usp_pax_check_out";
        //    SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
        //    para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
        //    para = cmd.Parameters.AddWithValue("@flt_date", strArray[2]);
        //    para = cmd.Parameters.AddWithValue("@op_airline", strArray[3]);
        //    para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[4]);
        //    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

        //    try
        //    {
        //        ret_code = TinyDAL.Exec(ref cmd);
        //        switch (ret_code)
        //        {
        //            case 0:
        //                lblError.Text = "OK.";
        //GridView1.DataBind();
        //                break;
        //            case 1:
        //                lblError.Text = "Flight not found!";
        //                break;
        //            case 2:
        //                lblError.Text = "Flight not found or flown!";
        //                break;
        //            case 3:
        //                lblError.Text = "Flight not open for checking-in!";
        //                break;
        //            case 10:
        //                lblError.Text = "Update ac_instance failed!";
        //                break;
        //            case 11:
        //                lblError.Text = "Update tickets failed!";
        //                break;
        //            default:
        //                lblError.Text = "Error "+ret_code.ToString()+"! Contact system adminstartor!";
        //                break;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //if (Application["DEBUG"].ToString() == "1")
        //    lblError.Text = ex.Message;
        //else
        //    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
        //    }

        //    
        //}
        //protected void btnGateAct_Click(object sender, EventArgs e)
        //{
        //    if (Session["ACL"].ToString().IndexOf("CC6") < 0)
        //    {
        //        lblError.Text = "Unauthorized!";
        //        return;
        //    }
        //    int ret_code;
        //    string key;
        //    string[] strArray;
        //    if (Request.Form["KEY"] == null)
        //    {
        //        lblError.Text = "Please select a record.";
        //        return;
        //    }
        //    key = Request.Form["KEY"];
        //    strArray = key.Split('|');

        //    SqlCommand cmd = new SqlCommand();
        //    lblError.Text = "";

        //    cmd.CommandText = "bll_tkt_set_status_at_gate";
        //    SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
        //    para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
        //    //para = cmd.Parameters.AddWithValue("@flt_date", strArray[2]);
        //    //para = cmd.Parameters.AddWithValue("@op_airline", strArray[3]);
        //    //para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[4]);
        //    para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
        //    para = cmd.Parameters.AddWithValue("@action", lstGateActions.SelectedValue);
        //    para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
        //    para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
        //    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
        //    para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
        //    try
        //    {
        //        ret_code = TinyDAL.Exec(ref cmd);
        //        switch (ret_code)
        //        {
        //            case 0:
        //                lblError.Text = "OK.";
        //                if (hidSearchWhat.Value == "O")
        //                    GridView1.DataSource = SqlDataSource1;
        //                else
        //                    GridView1.DataSource = SqlSearchID;
        //                GridView1.DataBind();
        //                break;
        //            case 1:
        //                lblError.Text = "Ticket not found!";
        //                break;
        //            case 2:
        //                lblError.Text = "Unknown action!";
        //                break;
        //            case 3:
        //                lblError.Text = "Trip not found!";
        //                break;
        //            case 4:
        //                lblError.Text = "Flight not proper status!";
        //                break;
        //            case 5:
        //            case 6:
        //            case 7:
        //                lblError.Text = "Pax not proper status!";
        //                break;
        //            case 10:
        //                lblError.Text = "Action failed!";
        //                break;
        //            case 50:
        //                lblError.Text = "Unauthorized airline!";
        //                break;
        //            default:
        //                lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
        //                break;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        if (Application["DEBUG"].ToString() == "1")
        //            lblError.Text = ex.Message;
        //        else
        //            lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
        //    }
        //}

        //protected void btnCounterAct_Click(object sender, EventArgs e)
        //{
        //    if (Session["ACL"].ToString().IndexOf("CC5") < 0)
        //    {
        //        lblError.Text = "Unauthorized!";
        //        return;
        //    }
        //    int ret_code;
        //    string key;
        //    string[] strArray;
        //    if (Request.Form["KEY"] == null)
        //    {
        //        lblError.Text = "Please select a record.";
        //        return;
        //    }
        //    key = Request.Form["KEY"];
        //    strArray = key.Split('|');

        //    SqlCommand cmd = new SqlCommand();
        //    lblError.Text = "";

        //    cmd.CommandText = "bll_tkt_set_status_at_counter";
        //    SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
        //    para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
        //    //para = cmd.Parameters.AddWithValue("@flt_date", strArray[2]);
        //    //para = cmd.Parameters.AddWithValue("@op_airline", strArray[3]);
        //    //para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[4]);
        //    para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
        //    para = cmd.Parameters.AddWithValue("@action", lstCounterActions.SelectedValue);
        //    para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
        //    para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
        //    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
        //    para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
        //    try
        //    {
        //        ret_code = TinyDAL.Exec(ref cmd);
        //        switch (ret_code)
        //        {
        //            case 0:
        //                lblError.Text = "OK.";
        //                if (hidSearchWhat.Value == "O")
        //                    GridView1.DataSource = SqlDataSource1;
        //                else
        //                    GridView1.DataSource = SqlSearchID;
        //                GridView1.DataBind();
        //                break;
        //            case 1:
        //                lblError.Text = "Trip not found!";
        //                break;
        //            case 2:
        //                lblError.Text = "Unknown action!";
        //                break;
        //            case 3:
        //                lblError.Text = "Flight not proper status!";
        //                break;
        //            case 4:
        //                lblError.Text = "Seat not found!";
        //                break;
        //            case 10:
        //                lblError.Text = "Release seat failed!";
        //                break;
        //            case 11:
        //                lblError.Text = "Check out failed!";
        //                break;
        //            case 12:
        //                lblError.Text = "Draw away failed!";
        //                break;
        //            case 50:
        //                lblError.Text = "Unauthorized airline!";
        //                break;
        //            case 60:
        //                lblError.Text = "Please unload the passenger's baggages first!";
        //                break;
        //            default:
        //                lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
        //                break;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        if (Application["DEBUG"].ToString() == "1")
        //            lblError.Text = ex.Message;
        //        else
        //            lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
        //    }
        //}
        protected void btnSetBag_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";

            cmd.CommandText = "bll_tkt_set_baggage";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
            para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
            //para = cmd.Parameters.AddWithValue("@flt_date", strArray[2]);
            //para = cmd.Parameters.AddWithValue("@op_airline", strArray[3]);
            //para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[4]);
            para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
            para = cmd.Parameters.AddWithValue("@bag_count", lstBagCount.SelectedValue);
            para = cmd.Parameters.AddWithValue("@bag_weight", txtWeight.Text);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        if (hidSearchWhat.Value == "O")
                            GridView1.DataSource = SqlDataSource1;
                        else
                            GridView1.DataSource = SqlSearchID;
                        GridView1.DataBind();
                        break;
                    case 2:
                        lblError.Text = "Trip not found!";
                        break;
                    case 1:
                        lblError.Text = "Setting failed!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized airline!";
                        break;
                    default:
                        lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnAddBag_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "bll_tkt_add_baggage";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
            para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
            //para = cmd.Parameters.AddWithValue("@flt_date", strArray[2]);
            //para = cmd.Parameters.AddWithValue("@op_airline", strArray[3]);
            //para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[4]);
            para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
            para = cmd.Parameters.AddWithValue("@bag_count", lstBagCount.SelectedValue);
            para = cmd.Parameters.AddWithValue("@bag_weight", txtWeight.Text);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        if (hidSearchWhat.Value == "O")
                            GridView1.DataSource = SqlDataSource1;
                        else
                            GridView1.DataSource = SqlSearchID;
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Adding failed!";
                        break;
                    case 2:
                        lblError.Text = "Ticket not found!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized airline!";
                        break;
                    default:
                        lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }

        protected void btnTktLogs_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the ticket";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            Response.Redirect("TicketLog.aspx?pnr=" + strArray[0] + "&pax_sn=" + strArray[1] + "&trip_sn=" + strArray[2]);
        }
        protected void btnPrnBoardingPass_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the ticket";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            Response.Redirect("BoardingPass.aspx?bk_id=" + strArray[0] + "&pax_list=" + strArray[1] + "&flt_date=" + hidFltDate.Value);
        }

        protected void btnPrintBagTag_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the ticket";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            Response.Redirect("BagTags.aspx?bk_id=" + strArray[0] + "&pax_sn=" + strArray[1] + "&trip_sn=" + strArray[2]);
        }


        protected void btnBatchChkIn_Click(object sender, EventArgs e)
        {
            //int i;
            //string output;
            //i=parse_list(txtPaxSn.Text, out output);
            //lblError.Text = output;
            //return;

            if (Session["ACL"].ToString().IndexOf("DB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            //if (hidSearchWhat.Value != "O")
            //{
            //    lblError.Text = "To batch check-in, please use order ID to search passengers!";
            //    return;
            //}

            int ret_code;
            //string key;
            //string pax_list;
            //string[] pax;
            //ret_code = num_list.parse(txtPaxSn.Text, out pax_list);
            //if (ret_code < 0)
            //{
            //    lblError.Text = "Bad format!";
            //    return;
            //}
            //pax = pax_list.Split(',');

            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            lblError.Text = "";
            cmd.CommandText = "bll_tkt_check_in";
            //lblError.Text = pax.GetLength(0).ToString();
            //return;
            CheckBox cbox;
            string pax_sn = "";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbox = (CheckBox)GridView1.Rows[i].Cells[1].FindControl("select");
                if (cbox.Checked)
                {
                    pax_sn = GridView1.Rows[i].Cells[23].Text;
                    cmd.Parameters.Clear();
                    //para = cmd.Parameters.AddWithValue("@bk_id", hidSearchNo.Value);
                    //para = cmd.Parameters.AddWithValue("@pax_sn", pax[i]);
                    para = cmd.Parameters.AddWithValue("@bk_id", GridView1.Rows[i].Cells[22].Text/*hidSearchNo.Value*/);
                    para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
                    para = cmd.Parameters.AddWithValue("@trip_sn", GridView1.Rows[i].Cells[24].Text);
                    //para = cmd.Parameters.AddWithValue("@flt_date", hidFltDate.Value);
                    para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
                    para = cmd.Parameters.AddWithValue("@auto_assign", 1);
                    para = cmd.Parameters.AddWithValue("@row", 0);
                    para = new SqlParameter("@col", SqlDbType.Char, 1);
                    para.Value = "";
                    cmd.Parameters.Add(para);
                    para = cmd.Parameters.AddWithValue("@bag_count", 0);
                    para = cmd.Parameters.AddWithValue("@bag_weight", 0);
                    para = cmd.Parameters.AddWithValue("@force_chk_in", 0);
                    para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                    para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                    para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                    para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                    try
                    {
                        ret_code = TinyDAL.Exec(ref cmd);
                        switch (ret_code)
                        {
                            case 0:
                                continue;
                                break;
                            case 1:
                                lblError.Text = pax_sn + ": Trip not found!";
                                return;
                            case 3:
                                lblError.Text = pax_sn + ": Flight not open for checking-in!";
                                return;
                            case 4:
                                lblError.Text = pax_sn + ": Ticket is expired!";
                                return;
                            case 10:
                                lblError.Text = pax_sn + ": Update ac_instance failed! Check seat status.";
                                return;
                            case 11:
                                lblError.Text = pax_sn + ": Update tickets failed! Please check pax status.";
                                return;
                            case 12:
                                lblError.Text = pax_sn + ": Can not find avail seat.";
                                return;
                            case 50:
                                lblError.Text = pax_sn + ": Unauthorized!";
                                return;
                            default:
                                lblError.Text = pax_sn + ": Error=" + ret_code.ToString() + "! Contact system adminstartor!";
                                return;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            lblError.Text = ex.Message;
                        else
                            lblError.Text = pax_sn + ": Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                        return;
                    }
                }
            }
            lblError.Text = "OK.";
            if (hidSearchWhat.Value == "O")
                GridView1.DataSource = SqlDataSource1;
            else
                GridView1.DataSource = SqlSearchID;
            GridView1.DataBind();
        }


        protected void btnBatchPrnPass_Click(object sender, EventArgs e)
        {
            //int ret_code;
            //string pax_list;
            //ret_code = num_list.parse(txtPaxSn.Text, out pax_list);
            //if (ret_code < 0)
            //{
            //    lblError.Text = "Bad format!";
            //    return;
            //}
            if (hidSearchWhat.Value != "O")
            {
                lblError.Text = "To batch print boarding-pass, please use order ID to search passengers!";
                return;
            }


            CheckBox cbox;
            string pax_list = "";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbox = (CheckBox)GridView1.Rows[i].Cells[1].FindControl("select");
                if (cbox.Checked)
                {
                    pax_list = pax_list + GridView1.Rows[i].Cells[24].Text + ","; //pax_sn
                }
            }
            if (pax_list.Length == 0)
            {
                lblError.Text = "Please check passengers first!";
                return;
            }
            pax_list = pax_list.Substring(0, pax_list.Length - 1);

            Response.Redirect("BoardingPass.aspx?bk_id=" + hidSearchNo.Value + "&pax_list=" + pax_list + "&flt_date=" + hidFltDate.Value);
        }
        protected void btnBatchPrnTags_Click(object sender, EventArgs e)
        {
            //行李只能单人打，要区分所属，而且团队往往将行李存在某一人名下！
            //int ret_code;
            //string pax_list;
            //ret_code = num_list.parse(txtPaxSn.Text, out pax_list);
            //if (ret_code < 0)
            //{
            //    lblError.Text = "Bad format!";
            //    return;
            //}
            //Response.Redirect("bag_tags.aspx?bk_id=" + hidOrderNo.Value + "&pax_list=" + pax_list + "&flt_date=" + hidFltDate.Value);

        }
        protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";

            cmd.CommandText = "bll_tkt_force_set_departure_status";// "bll_tkt_check_out";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
            para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
            para = cmd.Parameters.AddWithValue("@trip_sn", strArray[2]);
            para = cmd.Parameters.AddWithValue("@new_status", "O");
            para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        if (hidSearchWhat.Value == "O")
                            GridView1.DataSource = SqlDataSource1;
                        else
                            GridView1.DataSource = SqlSearchID;
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Trip not found!";
                        break;
                    case 2:
                        lblError.Text = "Flight not open for checking-out!";
                        break;
                    case 3:
                        lblError.Text = "Please use check-in interface to check-in!";
                        break;
                    case 4:
                        lblError.Text = "Please check-out all luggage first!";
                        break;
                    case 11:
                        lblError.Text = "Update tickets departure status failed!";
                        break;
                    case 12:
                        lblError.Text = "Release original seat failed!";
                        break;
                    case 13:
                        lblError.Text = "Check-out failed!";
                        break;
                    case 14:
                        lblError.Text = "Change status failed!";
                        break;
                    case 50:
                        lblError.Text = "Can not handle unauthorized airline ticket!";
                        break;
                    default:
                        lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void lbtnQueryID_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }
            //string pax_sn, bk_id;
            hidAirline.Value = txtAirline.Text;
            hidFltDate.Value = txtFltDate.Text;
            hidAirport.Value = txtAirport.Text;//.Trim().ToUpper();

            //txtTicketNo.Text = txtTicketNo.Text.Trim();
            //if (txtTicketNo.Text.Length != 14)
            //{
            //    lblError.Text = "Invalid ticket number!";
            //    return;
            //}
            //pax_sn = txtTicketNo.Text.Substring(txtTicketNo.Text.Length - 3);
            //bk_id = txtTicketNo.Text.Substring(0, txtTicketNo.Text.Length - 3);

            //hidOrderNo2.Value = txtTicketNo.Text.Substring(0, txtTicketNo.Text.Length - 3);
            //hidOrderNo.Value = hidOrderNo2.Value;
            //hidPaxSn.Value = txtTicketNo.Text.Substring(txtTicketNo.Text.Length - 3, 3);

            hidSearchNo.Value = txtNumber.Text;
            hidSearchNo2.Value = hidSearchNo.Value;
            hidSearchWhat.Value = "I";//ID
            hidSearchWhat2.Value = hidSearchWhat.Value;

            Session["FLT_DATE"] = txtFltDate.Text;
            Session["CHKIN_NO"] = hidSearchNo.Value;

            GridView1.DataSource = SqlSearchID;
            GridView1.DataBind();
        }
    }
}